<?php 
require_once '../includefiles.php';
require_once '../includes/download.php';
require_once '../model/nab.php';
require_once '../model/users.php';
require_once '../model/usermembership.php';
require_once '../model/usermembershipdetail.php';
//get detail
$uid = $_GET['uid'];
$invoice_id = $_GET['invoiceid'];

$Model_NAB = new Model_NAB();
$Model_User = new Model_Users();
//get User Detail
$objUser = $Model_User->getUserDetailsByUserId($uid);
//get order detail
$objInvoice = $Model_User->paymentHistoryDetailById($invoice_id);
//get NAB detail
$objNAB = $Model_NAB->getDetail();

$invoice_address = '<b>'.$objUser['companyname'].'</b><br>';
$invoice_address .= $objUser['first_name'] . ' ' .$objUser['last_name']."<br>";
$invoice_address .="Phone: ".$objUser['phone_number']."<br>";
$invoice_address .="Email: ".$objUser['email']."<br>";

if(!$objInvoice['total'])
	$temp_total = $objInvoice['amount']; 
else
	$temp_total=$objInvoice['total'];
	
$tax = ($temp_total*10)/110;
$subtotal = $temp_total-$tax;

$orderitems = $userObj->getOrderItems($invoice_id);

if($orderitems)
{
	$i=1;
	foreach($orderitems as $oi)
	{
		$itemlist .='<tr>';
		$itemlist .='<td  align="center">'.$i.'</td>';
		$itemlist .='<td>'.$oi['name'].'</td>';
		$itemlist .='<td  align="center">1</td>';
		$itemlist .='<td  align="right">$'.number_format($oi['price'],2).'</td>';
		$itemlist .='<td  align="right">$'.number_format($oi['price'],2).'</td>';
		$itemlist .='</tr>';
		$i++;
	}	
}
else
{
	$itemlist = '<tr>';
	$itemlist .='<td  align="center">1</td>';
	$itemlist .='<td>'.$objInvoice['paymentfor'].'</td>';
	$itemlist .='<td  align="center">1</td>';
	$itemlist .='<td  align="right">$'.number_format($objInvoice['amount'],2).'</td>';
	$itemlist .='<td  align="right">$'.number_format($objInvoice['amount'],2).'</td>';
	$itemlist .='</tr>';	
}

$params = array();
$params['date'] = date('M d, Y');
$params['invoice_number'] = $objInvoice['invoiceid'];
$params['invoice_to'] = $invoice_to;
$params['invoice_from'] = $objNAB['invoice_for'];
$params['invoice_address'] = $invoice_address;
$params['subtotal'] = number_format($subtotal,2);
$params['tax'] = number_format($tax,2);
$params['total'] = number_format($temp_total,2);
$params['itemlist'] = $itemlist;

$filename = $objInvoice['invoiceid'].'.pdf';
$path = BASEFOLDER.'/maker/';
$maker = new PdfGenHelper();
$maker->genInvoice($params,$path,$filename);
if($_GET['targ']!=='attach')
{
	$downloader = new FileDownload();
	$downloader->do_download($filename,$path);
	@unlink($path.$filename);
}

class PdfGenHelper{
	public function genInvoice($params,$path,$filename)
	{
		// récupération du contenu HTML
		ob_start();
		include(dirname(__FILE__).'/html2pdf/template.php');
		$content = ob_get_clean();
		$date = date('M d, Y');
		$content = str_replace("{date}",$date,$content);
		$content = str_replace("{invoice_number}",$params['invoice_number'],$content);
		$content = str_replace("{invoice_to}",$params['invoice_to'],$content);
		$content = str_replace("{invoice_for}",$params['invoice_for'],$content);
		$content = str_replace("{invoice_address}",$params['invoice_address'],$content);
		$content = str_replace("{coupon_code}",$params['coupon_code'],$content);
		$content = str_replace("{coupon_value}",$params['coupon_value'],$content);
		$content = str_replace("{subtotal}",$params['subtotal'],$content);
		$content = str_replace("{total}",$params['total'],$content);
		$content = str_replace("{item_list}",$params['itemlist'],$content);
		$content = str_replace("{tax}",$params['tax'],$content);
		// conversion HTML => PDF
		require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
		$html2pdf = new HTML2PDF('P','A4','fr');
	//	$html2pdf->setModeDebug();
		$html2pdf->setDefaultFont('helvetica');
		$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
		$html2pdf->Output($path.$filename,'F');
	}

}

?>